<template>
  <div id="app-vino">
    <div class="login-box-vino" :style="backgroundDiv">
      <div :style="{'height':height}">
        <div class="login-vino">
          <!-- 登录title -->
          <p class="loginTitle-vino">登录系统</p>
          <el-divider></el-divider>
          <!-- 输入框 -->
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            class="demo-ruleForm"
          >
            <el-form-item prop="id">
              <el-input
                v-model.number="ruleForm.id"
                class="inputName-vino"
                placeholder="| 账号"
                prefix-icon="el-icon-user"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item prop="pass">
              <el-input
                placeholder="| 密码"
                prefix-icon="el-icon-lock"
                type="password"
                v-model="ruleForm.pass"
                autocomplete="off"
                class="inputPass-vino"
              ></el-input>
            </el-form-item>
          </el-form>
          <!-- 登录按钮 -->
          <p class="loginNow-vino" @click="loginNow">登录</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import qs from "qs";
export default {
  data() {
    // 验证账号
    var checkId = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          callback();
        }
      }, 500);
    };
    // 验证密码
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    
    return {
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        id: [{ validator: checkId, trigger: "blur" }],
      },
      ruleForm: {
        pass: "",
        checkPass: "",
        id: "",
      },
      height: window.innerHeight - 20 + "px",
      inputId: "",
      inputPassWord: "",
      backgroundDiv: {
        backgroundImage:
          "url(" + require("../../../public/img/bgindex.jpg") + ")",
        backgroundRepeat: "no-repeat",
        backgroundSize: "100% 100%",
      },
    };
  },
  methods: {
    loginNow() {
      let that = this;
      // let obj = new FormData();
      // obj.append("empid", this.ruleForm.id);
      // obj.append("password", this.ruleForm.id);
      that
        .$axios({
          method: "post",
          url: "/api/login",
          // data: qs.stringify({empid:"1",password: "123"}),
          data: qs.stringify({
            empid: this.ruleForm.id,
            password: this.ruleForm.pass,
          }),
          headers: {
            // "Content-Type": "application/json;charset=UTF-8",
            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
          },
        })
        .then((data) => {
          console.log(data.data.data);
          
          if (data.data.code == -1) {
            this.$message.error("账号或密码错误!");
          } else {
            this.$message({
              message: "登录成功!",
              type: "success",
            });
            sessionStorage.setItem('loginState',true);
            sessionStorage.setItem('empid',data.data.data);

            this.$router.push("/index");
            // this.$store.state.loginstate = true
          
          }
        })
        .catch((error) => {
          this.$message.error("登录失败!");
        });
    },
  },
  mounted() {
    this.height = window.innerHeight - 20 + "px";
  },
};
</script>

<style lang="less">
.loginNow-vino {
  background-color: rgb(147, 226, 244);
  height: 15%;
  line-height: 45px;
  font-size: 20px;
  color: white;
  border-radius: 6px;
  margin-top: 15%;
}
.loginTitle-vino {
  font-size: 20px;
}
.inputName-vino {
  margin-bottom: 6%;
  margin-top: 5%;
}
#app-vino {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  text-align: center;
  color: #2c3e50;
}
.login-vino {
  padding: 20px;
  box-shadow: 0 0 1px 1px silver;
  width: 300px;
  height: 320px;
  border-radius: 10px;
  position: absolute;
  right: 10%;
  top: 20%;
  background-color: white;
}
</style>
